package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.hjq.http.listener.HttpCallback;
import com.hjq.shape.layout.ShapeLinearLayout;
import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.db.LLControllerDBManager;
import com.qiantu.api.entity.ControllerBean;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.PanelBindConfig;
import com.qiantu.api.entity.PanelBindConfigs;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PanelBindConfigActivity extends AppActivity {
    private int SCENE_BIND_COUNT = 12;
    private int LIGHT_BIND_COUNT = 64;
    private int CURTAIN_BIND_COUNT = 8;
    private int AIR_CONDITIONER_COUNT = 8;
    private int FLOOR_HEATING_BIND_COUNT = 8;
    private int FRESH_AIR_BIND_COUNT = 8;
    private List<PanelBindConfig> mSceneConfigData = new ArrayList<>();
    private List<PanelBindConfig> mLightConfigData = new ArrayList<>();
    private List<PanelBindConfig> mCurtainConfigData = new ArrayList<>();
    private List<PanelBindConfig> mAirConditionerConfigData = new ArrayList<>();
    private List<PanelBindConfig> mFloorHeatingConfigData = new ArrayList<>();
    private List<PanelBindConfig> mFreshAirConfigData = new ArrayList<>();
    private ShapeLinearLayout mSceneLayout;
    private ShapeLinearLayout mLightLayout;
    private ShapeLinearLayout mCurtainLayout;
    private SettingBar mSceneConfig;
    private SettingBar mLightConfig;
    private SettingBar mCurtainConfig;
    private SettingBar mAirConditionerType;
    private SettingBar mFloorHeatingType;
    private SettingBar mFreshAirConfig;
    private String controllerSerialNo;
    private String productModel;
    @Log
    public static void start(Context context, String controllerSerialNo) {
        Intent intent = new Intent(context, PanelBindConfigActivity.class);
        intent.putExtra("controllerSerialNo", controllerSerialNo);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_panel_bind_config;
    }

    @Override
    public void initView() {
        mSceneLayout = findViewById(R.id.scene_config_shape_layout);
        mSceneConfig = findViewById(R.id.scene_config);
        mLightLayout = findViewById(R.id.light_config_shape_layout);
        mLightConfig = findViewById(R.id.light_config);
        mCurtainLayout = findViewById(R.id.curtain_config_shape_layout);
        mCurtainConfig = findViewById(R.id.curtain_config);
        mAirConditionerType = findViewById(R.id.air_conditioner_type);
        mFloorHeatingType = findViewById(R.id.floor_heating_type);
        mFreshAirConfig = findViewById(R.id.fresh_air_config);
        setOnClickListener(mSceneConfig, mLightConfig, mCurtainConfig, mAirConditionerType, mFreshAirConfig, mFloorHeatingType);
    }

    @Override
    protected void initData() {
        controllerSerialNo = getString("controllerSerialNo");
        ControllerBean controllerBean = LLControllerDBManager.getInstance(getContext()).getControllerBySerialNo(controllerSerialNo);
        productModel = controllerBean.getProductModel();
        if ("0x1005".equals(productModel) || "0x0554".equals(productModel)|| "0x053D".equals(productModel)){
            mSceneLayout.setVisibility(View.GONE);
            mLightLayout.setVisibility(View.GONE);
            mCurtainLayout.setVisibility(View.GONE);
        }
        if("0x0554".equals(productModel)|| "0x053D".equals(productModel)){
            AIR_CONDITIONER_COUNT = 1;
            FRESH_AIR_BIND_COUNT = 1;
            FLOOR_HEATING_BIND_COUNT = 1;
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        LLHttpManager.getPanelBindConfig(getAppActivity(), controllerSerialNo, new HttpCallback<HttpData<PanelBindConfigs>>(getAppActivity()){
            @Override
            public void onSucceed(HttpData<PanelBindConfigs> result) {
                super.onSucceed(result);
                mLightConfigData = new ArrayList<>();
                mAirConditionerConfigData = new ArrayList<>();
                mFreshAirConfigData = new ArrayList<>();
                mCurtainConfigData = new ArrayList<>();
                mSceneConfigData = new ArrayList<>();
                mFloorHeatingConfigData = new ArrayList<>();
                for (PanelBindConfig config : result.getData().getPanelBindConfigs()){
                    if (config.getBindUIType() == 1){
                        mLightConfigData.add(config);
                    }else if (config.getBindUIType() == 2){
                        mAirConditionerConfigData.add(config);
                    }else if (config.getBindUIType() == 3){
                        mFreshAirConfigData.add(config);
                    }else if (config.getBindUIType() == 4){
                        mCurtainConfigData.add(config);
                    }else if (config.getBindUIType() == 5){
                        mSceneConfigData.add(config);
                    }else if (config.getBindUIType() == 6){
                        mFloorHeatingConfigData.add(config);
                    }
                }
                updateView();
            }
        });
    }

    private void updateView() {
        mSceneConfig.setRightText(mSceneConfigData.size() + "/" + SCENE_BIND_COUNT);
        mLightConfig.setRightText(mLightConfigData.size() + "/" + LIGHT_BIND_COUNT);
        mCurtainConfig.setRightText(mCurtainConfigData.size() + "/" + CURTAIN_BIND_COUNT);
        mAirConditionerType.setRightText(mAirConditionerConfigData.size() + "/" + AIR_CONDITIONER_COUNT);
        mFloorHeatingType.setRightText(mFloorHeatingConfigData.size() + "/" + FLOOR_HEATING_BIND_COUNT);
        mFreshAirConfig.setRightText(mFreshAirConfigData.size() + "/" + FRESH_AIR_BIND_COUNT);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        Intent intent = new Intent(this,PanelBindDeviceActivity.class);
        intent.putExtra("controllerSerialNo",controllerSerialNo);
        if (view == mSceneConfig){
            intent.putExtra("type","scene");
            intent.putExtra("count",SCENE_BIND_COUNT);
            intent.putExtra("bindUIType",5);
            intent.putExtra("bindType",7);
            intent.putExtra("data", (Serializable) mSceneConfigData);
//            PanelBindStep1.start(getAppActivity(), controllerSerialNo, productModel, 5, mSceneConfig.getLeftDrawableResId(), getString(R.string.scene), SCENE_BIND_COUNT, mSceneConfigData);
        }else if (view == mLightConfig){
//            PanelBindStep1.start(getAppActivity(), controllerSerialNo, productModel, 1, mLightConfig.getLeftDrawableResId(), getString(R.string.type_light), LIGHT_BIND_COUNT, mLightConfigData);
            intent.putExtra("type","light");
            intent.putExtra("count",LIGHT_BIND_COUNT);
            intent.putExtra("bindUIType",1);
            intent.putExtra("bindType",1);
            intent.putExtra("data", (Serializable) mLightConfigData);
        }else if (view == mCurtainConfig){
//            PanelBindStep1.start(getAppActivity(), controllerSerialNo, productModel, 4, mCurtainConfig.getLeftDrawableResId(), getString(R.string.curtain), CURTAIN_BIND_COUNT, mCurtainConfigData);
            intent.putExtra("type","curtain");
            intent.putExtra("count",CURTAIN_BIND_COUNT);
            intent.putExtra("bindUIType",4);
            intent.putExtra("bindType",5);
            intent.putExtra("data", (Serializable) mCurtainConfigData);
        }else if (view == mAirConditionerType){
//            PanelBindStep1.start(getAppActivity(), controllerSerialNo, productModel, 2, mAirConditionerType.getLeftDrawableResId(), getString(R.string.air_conditioner), AIR_CONDITIONER_COUNT, mAirConditionerConfigData);
            intent.putExtra("type","airconditioner");
            intent.putExtra("count",AIR_CONDITIONER_COUNT);
            intent.putExtra("bindUIType",2);
            intent.putExtra("bindType",3);
            intent.putExtra("data", (Serializable) mAirConditionerConfigData);
        }else if (view == mFreshAirConfig){
//            PanelBindStep1.start(getAppActivity(), controllerSerialNo, productModel, 3, mFreshAirConfig.getLeftDrawableResId(), getString(R.string.fresh_air), FRESH_AIR_BIND_COUNT, mFreshAirConfigData);
            intent.putExtra("type","freshair");
            intent.putExtra("count",FRESH_AIR_BIND_COUNT);
            intent.putExtra("bindUIType",3);
            intent.putExtra("bindType",4);
            intent.putExtra("data", (Serializable) mFreshAirConfigData );
        }else if (view == mFloorHeatingType){
//            PanelBindStep1.start(getAppActivity(), controllerSerialNo, productModel, 6, mFloorHeatingType.getLeftDrawableResId(), getString(R.string.floor_heating), FLOOR_HEATING_BIND_COUNT, mFloorHeatingConfigData);
            intent.putExtra("type","floorheating");
            intent.putExtra("count",FLOOR_HEATING_BIND_COUNT);
            intent.putExtra("bindUIType",6);
            intent.putExtra("bindType",8);
            intent.putExtra("data", (Serializable) mFloorHeatingConfigData);
        }
        startActivity(intent);
    }

    public static class UIPanelBindConfig{
        PanelBindConfig config;
        SceneBean sceneBean;
        DeviceBean deviceBean;
    }
}